package com.wtx.atguigu.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: MappedByteBuffer: 可以让文件直接在内存中修改, 操作系统不需要拷贝一次
 * @author: wtx
 * @createDate: 2020/6/8
 */
public class MappedByteBufferTest {
    public static void main(String[] args) throws IOException {

        RandomAccessFile randomAccessFile = new RandomAccessFile("file01.txt", "rw");
        FileChannel channel = randomAccessFile.getChannel();

        /*
        * FileChannel.MapMode.READ_WRITE: 读写模式
        * 0: 可以修改的起始位置
        * 5: randomAccessFile映射到内存的字节大小
        * */
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);

        // 去文件的目录查看, 被修改, 直接在项目路径下查看, 还是h
        mappedByteBuffer.put(0,(byte)'H');
        randomAccessFile.close();
    }
}
